package com.kjsdfgk.hjmdsf.ftmztlist.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.kjsdfgk.hjmdsf.app.entity.Account;
import com.kjsdfgk.hjmdsf.app.entity.WebPhoto;
import com.kjsdfgk.hjmdsf.app.entity.XJVideoJson;
import com.kjsdfgk.hjmdsf.app.msg.AddViewMsg;
import com.kjsdfgk.hjmdsf.app.utils.SharedPrefsUtils;
import com.kjsdfgk.hjmdsf.ftmztlist.di.component.DaggerMZTListComponent;
import com.kjsdfgk.hjmdsf.ftmztlist.mvp.contract.MZTListContract;
import com.kjsdfgk.hjmdsf.ftmztlist.mvp.presenter.MZTListPresenter;

import com.kjsdfgk.hjmdsf.myphoto.mvp.ui.activity.MyPhotoActivity;
import com.kjsdfgk.hjmdsf.play.mvp.ui.activity.MyPlayVideoActivity;
import com.lksdkjs.live.R;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 05/07/2020 22:13
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MZTListFragment extends BaseFragment<MZTListPresenter> implements MZTListContract.View, SwipeRefreshLayout.OnRefreshListener {
    String typeValue ;

    public MZTListFragment() {

    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMZTListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mztlist, container, false);
    }
    @BindView(R.id.contentView)
    SwipeRefreshLayout mSwipeRefreshLayout;
    int page;
    WebPhotoAdapter webPhotoAdapter;
    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        typeValue = bundle.getString("category");

        page = SharedPrefsUtils.getInt(typeValue , 1);
        mPresenter.loadImages(page ,typeValue);

        GridLayoutManager layoutManager = new GridLayoutManager(getContext(),2);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(webPhotoAdapter = new WebPhotoAdapter(R.layout.item_web_photo));
        mSwipeRefreshLayout.setOnRefreshListener(this);
        webPhotoAdapter.setOnLoadMoreListener(() -> {
            mPresenter.loadImagesMore(typeValue);
        });
        webPhotoAdapter.setOnItemClickListener((adapter, view, position) -> {
          WebPhoto webPhoto = (WebPhoto) adapter.getData().get(position);

            Account account = mPresenter.getAccount();
            if (account != null){
                int isVip = SharedPrefsUtils.getInt("isVip" , 1000);
                if (isVip == 200){
                    Intent intent = new Intent(getActivity() , MyPhotoActivity.class);
                    intent.putExtra("webPhoto",webPhoto);
                    getActivity().startActivity(intent );
                }else {
                    long t1 = Long.parseLong(account.getVip())*1000 ;
                    long t2 = System.currentTimeMillis() ;
                    if ( t1 > t2){
                        Intent intent = new Intent(getActivity() , MyPhotoActivity.class);
                        intent.putExtra("webPhoto",webPhoto);
                        getActivity().startActivity(intent );
                    }else {
                        EventBus.getDefault().post(new AddViewMsg("showMemberDlg",""));
                    }
                }

            } else {
                EventBus.getDefault().post(new AddViewMsg("showLoginDlg",""));
            }

        });

//        handler.sendEmptyMessageDelayed(1 , 10000);

    }
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            int mypage = SharedPrefsUtils.getInt(typeValue , 1);
            mPresenter.downloadServerData(typeValue ,mypage);
        }
    };
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading(String key) {
        mSwipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        mSwipeRefreshLayout.setRefreshing(false);


    }

    @Override
    public boolean isCancel() {
        return false;
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @Override
    public void showWebPhoto(List<WebPhoto> photoList) {
        webPhotoAdapter.setNewData(photoList);
    }

    @Override
    public void showWebPhotoMore(List<WebPhoto> photoList) {
        if (photoList!=null)
        webPhotoAdapter.addData(photoList);
        webPhotoAdapter.loadMoreComplete();
    }

    @Override
    public void saveAndgoNext(int page) {

        handler.sendEmptyMessageDelayed(1 , 10000);

    }

    @Override
    public void onRefresh() {
        mPresenter.loadImages(page ,typeValue);
    }
}
